// src/mixins/tabbar-notice.js
import {
    unReadCount
} from '@/api/custom.js';
export default {
    methods: {
      async checkUnReadMessage() {
        try {
          const res = await unReadCount();
          console.log("getUnReadCount", res);
          
          if (res.code === 200) {
            const { unReadCount, hasUnRead } = res.data;
            const tabbarIndex = 2; // 对应消息页的tabbar索引
  
            if (unReadCount > 0) {
              uni.setTabBarBadge({
                index: tabbarIndex,
                text: unReadCount.toString()
              });
              uni.showTabBarRedDot({ index: tabbarIndex });
            } else {
              // uni.removeTabBarBadge({ index: tabbarIndex });
              hasUnRead 
                ? uni.showTabBarRedDot({ index: tabbarIndex })
                : uni.hideTabBarRedDot({ index: tabbarIndex });
            }
            return res.data.unReadCount > 0 || res.data.hasUnRead; // 返回是否有未读消息
          }
        } catch (e) {
          console.error('检查未读消息失败', e);
          return false;
        }
      }
    },
    
    // 自动绑定到页面生命周期（可选）
    onShow() {
      this.checkUnReadMessage();
    }
  }
  